package cn.charlotte.pit.manager;

import java.util.LinkedHashMap;
import java.util.Map;

public class ResourceManager {

    protected double percentage = 1;
    private int maxExp = 250;
    private int maxGold = 2500;
    private final Map<String, Double> expAdditions = new LinkedHashMap<>();
    private final Map<String, Double> expModifiers = new LinkedHashMap<>();
    private final Map<String, Double> goldAdditions = new LinkedHashMap<>();
    private final Map<String, Double> goldModifiers = new LinkedHashMap<>();

//    public double calculateEXP() {
//        double exp = 0;
//
//        double mod = 1.0d;
//        for (Double value : expModifiers.values()) {
//            mod *= value;
//        }
//        for (Map.Entry<String, Double> entry : expAdditions.entrySet()) {
//            exp += entry.getValue() * mod;
//        }
//
//        return Math.min(Math.ceil(exp), maxExp);
//    }
    public double calculateEXP() {
        double baseExp = 0;
        for (Map.Entry<String, Double> entry : expAdditions.entrySet()) {
            baseExp += entry.getValue();
        }

        double bonus = 0.0;   // 加成总和（modifier > 1）
        double penalty = 1.0; // 减益乘积（modifier < 1）

        for (Double value : expModifiers.values()) {
            if (value > 1.0) {
                bonus += (value - 1.0);
            } else if (value < 1.0) {
                penalty *= value;
            }
            // value == 1.0 时忽略
        }

        double finalModifier = (1.0 + bonus) * penalty;
        double exp = baseExp * finalModifier;

        return Math.min(Math.ceil(exp), maxExp);
    }

    public void clearExp() {
        expAdditions.clear();
    }

    public double calculateGold() {
        double baseGold = 0.0;

        // 计算基础金币
        for (Double value : goldAdditions.values()) {
            baseGold += value;
        }

        // 分类处理增益和减益
        double bonus = 0.0;
        double penalty = 1.0;
        for (Double value : goldModifiers.values()) {
            if (value > 1.0) {
                bonus += (value - 1.0);
            } else if (value < 1.0) {
                penalty *= value;
            }
            // value == 1.0 可跳过，无增减影响
        }

        // 最终倍率 = (1 + 总增益) * 总减益
        double finalModifier = (1.0 + bonus) * penalty;

        // 应用倍率
        double gold = baseGold * finalModifier;

        // 限制最大金币值
        if (gold > maxGold) {
            gold = maxGold;
        }

        return gold;
    }


    public void setMaxExp(int maxExp) {
        this.maxExp = maxExp;
    }

    public void setMaxGold(int maxGold) {
        this.maxGold = maxGold;
    }

    public void addExp(double exp, String reason) {
        expAdditions.put(reason, exp);
    }

    public void addExpModifier(double modifier, String reason) {
        expModifiers.put(reason, modifier);
    }

    public void addGold(double gold, String reason) {
        goldAdditions.put(reason, gold);
    }

    public void addGoldModifier(double modifier, String reason) {
        goldModifiers.put(reason, modifier);
    }

    public Map<String, Double> getExpAdditions() {
        return expAdditions;
    }

    public Map<String, Double> getExpModifiers() {
        return expModifiers;
    }

    public Map<String, Double> getGoldAdditions() {
        return goldAdditions;
    }

    public Map<String, Double> getGoldModifiers() {
        return goldModifiers;
    }

    public int getMaxExp() {
        return maxExp;
    }

    public int getMaxGold() {
        return maxGold;
    }
}
